package com.example.mq.domo;

import com.example.mq.mqchile.Channel;
import com.example.mq.mqchile.Connection;
import com.example.mq.mqchile.ConnectionFactory;
import com.example.mq.mqserver.core.ExchangeType;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: DongGuoZhen
 * @Date: 2024/03/09/12:19
 * @Description: 这个类表示生产者  通常这是一个服务器程序
 */
public class DomoProducer {
    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("启动生产者");
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(9090);

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        //创建交换机和队列
        channel.exchangeDeclare("testExchange", ExchangeType.DIRECT,true,false,null);
        channel.queueDeclare("testQueue",true,false,false,null);
        //创建一个消息进行发送
        byte[] body = "hello".getBytes();
        boolean ok = channel.basicPublish("testExchange","testQueue",null,body);
        System.out.println("消息投递完成 ok=" + ok);

        Thread.sleep(500);
        channel.close();
        connection.close();
    }
}
